/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.operativa;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoSegContException;
import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoSegContRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.service.IComparendoSegContService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.ComparendoSegCont;

/**
 * @author Usuario
 * 
 */
public class ComparendoSegContController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public ComparendoSegContController() {
		this.comparendoSegContListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda ComparendoSegCont */
	public ComparendoSegCont comparendoSegContSearch = new ComparendoSegCont();

	/** Objeto contenedor del listado de ComparendoSegCont luego de una consulta. */
	private DataModel comparendoSegContListModel;

	/** bean para el manejo del formulario de creacion/edicion. ComparendoSegCont */
	private ComparendoSegCont comparendoSegCont = new ComparendoSegCont();

	/** instancia de servicio comparendoSegContService */
	private IComparendoSegContService comparendoSegContService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchComparendoSegContByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getComparendoSegContService().load(1, 10,
					this.comparendoSegContSearch);
			this.comparendoSegContListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoSegContListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoSegContListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}


	/**
	 * 
	 * @return String
	 */
	public String searchComparendoSegContByRadicado() {
		String msg = "";
		try {
			List valores = null;
			valores = getComparendoSegContService().loadByRadicado(1, 10,
					this.comparendoSegContSearch);
			this.comparendoSegContListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoSegContListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoSegContListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}
	

	/**
	 * 
	 * @return String
	 */
	public String searchComparendoSegContByRadicado(IComparendoSegContService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.loadByRadicado(1, 10,
					this.comparendoSegContSearch);
			this.comparendoSegContListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoSegContListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoSegContListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}	
		
	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.comparendoSegCont.getId().longValue() == 0L) {
				getComparendoSegContService().insert(this.comparendoSegCont);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ComparendoSegContException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ComparendoSegContRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- comparendoSegCont " + msg, null, null);
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.comparendoSegCont.getId().longValue() != 0L) {
				getComparendoSegContService().update(this.comparendoSegCont);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ComparendoSegContException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ComparendoSegContRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createComparendoSegCont() {
		comparendoSegCont = new ComparendoSegCont();
		comparendoSegCont.setId(new Long(0L));
		comparendoSegCont.setCodigo("");
		comparendoSegCont.setIdComparendo(comparendoSegContSearch.getIdComparendo());
		//comparendoSegCont.setValor("");
		//comparendoSegCont.setActivo("N");
		return AppConstants.COMPARENDO_SEG_CONT_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateComparendoSegCont() {
		Map params = getExternalContext().getRequestParameterMap();
		String comparendoSegContDataIndex = (String) params
				.get("comparendoSegContDataIndex");
		this.comparendoSegCont = (ComparendoSegCont) ((List) this.comparendoSegContListModel
				.getWrappedData())
				.get(Integer.parseInt(comparendoSegContDataIndex));
		return AppConstants.COMPARENDO_SEG_CONT_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelComparendoSegCont() {
		return AppConstants.COMPARENDO_SEG_CONT_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public ComparendoSegCont getComparendoSegContSearch() {
		return comparendoSegContSearch;
	}

	/**
	 * 
	 * @param comparendoSegContSearch
	 */
	public void setcomparendoSegContSearch(ComparendoSegCont comparendoSegContSearch) {
		this.comparendoSegContSearch = comparendoSegContSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getcomparendoSegContListModel() {
		return comparendoSegContListModel;
	}

	/**
	 * 
	 * @param comparendoSegContListModel
	 */
	public void setcomparendoSegContListModel(DataModel comparendoSegContListModel) {
		this.comparendoSegContListModel = comparendoSegContListModel;
	}

	/**
	 * 
	 * @return
	 */
	public ComparendoSegCont getComparendoSegCont() {
		return comparendoSegCont;
	}

	/**
	 * 
	 * @param comparendoSegCont
	 */
	public void setcomparendoSegCont(ComparendoSegCont comparendoSegCont) {
		this.comparendoSegCont = comparendoSegCont;
	}

	/**
	 * obtencion del servicio comparendoSegContService
	 * 
	 * @return
	 */
	public IComparendoSegContService getComparendoSegContService() {
		return comparendoSegContService;
	}

	/**
	 * establecimiento del servicio comparendoSegContService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param comparendoSegContService
	 */
	public void setComparendoSegContService(
			IComparendoSegContService comparendoSegContService) {
		this.comparendoSegContService = comparendoSegContService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
